//
//  ViewController.swift
//  08RandomColor
//
//  Created by Clover on 2018/4/17.
//  Copyright © 2018年 youbing. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    let gradientLayer = CAGradientLayer()
    
    var timer : Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.shared.isStatusBarHidden =  true
    }
    
    @IBAction func play(_ sender: Any) {
        if timer == nil {
            timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(randomColor), userInfo: nil, repeats: true)
        }
        gradientLayer.frame = self.view.bounds
        let color1 = UIColor(white: 0.5, alpha: 0.2).cgColor as CGColor
        let color2 = UIColor(red: 1.0, green: 0.4, blue: 0.2, alpha: 0.2).cgColor as CGColor
        let color3 = UIColor(red: 0.3, green: 0.3, blue: 0.5, alpha: 0.8).cgColor
        
        gradientLayer.colors = [color1,color2,color3]
        gradientLayer.locations = [0.1,0.4,0.6]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 1)
        self.view.layer.addSublayer(gradientLayer)
        
    }
    
    @objc func randomColor() {
        // 随机色
        let redValue = CGFloat(drand48())
        let blueValue =  CGFloat(drand48())
        let greenValue = CGFloat(drand48())
        
        
        self.view.backgroundColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
        
    }
}

